package cn.token.token.controller;

import cn.token.sconst.SystemConst;
import cn.token.sconst.TokenUtil;
import cn.token.token.aop.UserLoginToken;
import org.apache.catalina.Session;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * @Auther: qijiang
 * @Date: 2018/10/26 09:33
 * @Description:
 */

@RestController
public class UserApi {
//    @Autowired
//    UserService userService;
//    @Autowired
//    TokenService tokenService;
    //登录
    @PostMapping("/login")
    public Object login(@RequestParam String userName , @RequestParam String userPass, HttpServletRequest request){
        //JSONObject jsonObject=new JSONObject();
        //User userForBase=userService.findByUsername(user);
        if(userName==null){
            //jsonObject.put("message","登录失败,用户不存在");
            return "登录失败,用户不存在";
        }else {
            if (!userPass.equals(SystemConst.userPass)){
                //jsonObject.put("message","登录失败,密码错误");
                return "登录失败,密码错误";
            }else {
                //String token = tokenService.getToken(userForBase);
                HttpSession session = request.getSession();
                String sessionId = session.getId();
                System.out.println("sessionId"+sessionId);
                Cookie[] cookies = request.getCookies();
                if(null == cookies){

                }
                for (Cookie c:cookies) {

                }
                UUID uuid = UUID.randomUUID();
                request.getSession().setAttribute("token_key",uuid);
                String token = TokenUtil.getToken(uuid.toString());
//                jsonObject.put("token", token);
//                jsonObject.put("user", userForBase);
                return token;
            }
        }
    }
    @UserLoginToken
    @GetMapping("/getMessage")
    public String getMessage(){
        return "你已通过验证";
    }

    @GetMapping("/test")
    public String test(){
        return "test";
    }
}